package com.aceitunaproject.clientinterface.eventadapters;

import java.util.LinkedList;
import java.util.List;
import com.aceitunaproject.shared.message.Message;

/**
 * 
 * Basic abstract implementation of the event handler adapter, contains common
 * functionality
 * 
 * @author Federico Bergstein.
 * 
 */
abstract class BasicAceitunaEventAdapter implements AceitunaEventAdapter {

	/**
	 * The list of registered handlers for this event.
	 * 
	 */
	List<Object> handlers = new LinkedList<Object>();

	/**
	 * Constructor
	 */
	public BasicAceitunaEventAdapter() {

	}

	/**
	 * Constructor for the class
	 * 
	 * @param handlers
	 *            the event handlers to adapt
	 */
	public BasicAceitunaEventAdapter(List<Object> handlers) {
		this.handlers = handlers;
	}

	public void trigger(Message message) {
		for (Object handler : handlers) {
			triggerInHandler(handler, message);
		}

	}

	public List<Object> getHandlers() {
		return handlers;
	}

	/**
	 * Triggers the event in the handler, using the handler's method (IE:
	 * ChatMessageReceived) In concrete implementations of this class the
	 * message MUST be the type expected by the handler or a runtime error will
	 * be triggered
	 * 
	 * @param handler
	 *            the handler whose event will be triggered
	 * @param message
	 *            the message received from the server with the event
	 */
	abstract void triggerInHandler(Object handler, Message message);
}

// TODO: consider using generics